/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.anniversary.service.impl;

import me.zhengjie.modules.anniversary.domain.GzmuAnnUserContacts;
import me.zhengjie.modules.anniversary.domain.Student;
import me.zhengjie.modules.anniversary.service.dto.StudentDto;
import me.zhengjie.modules.system.domain.Dept;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.anniversary.repository.GzmuAnnUserContactsRepository;
import me.zhengjie.modules.anniversary.service.GzmuAnnUserContactsService;
import me.zhengjie.modules.anniversary.service.dto.GzmuAnnUserContactsDto;
import me.zhengjie.modules.anniversary.service.dto.GzmuAnnUserContactsQueryCriteria;
import me.zhengjie.modules.anniversary.service.mapstruct.GzmuAnnUserContactsMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;

import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author yangyao
* @date 2020-09-20
**/
@Service
@RequiredArgsConstructor
public class GzmuAnnUserContactsServiceImpl implements GzmuAnnUserContactsService {

    private final GzmuAnnUserContactsRepository gzmuAnnUserContactsRepository;
    private final GzmuAnnUserContactsMapper gzmuAnnUserContactsMapper;

    @Override
    public Map<String,Object> queryAll(GzmuAnnUserContactsQueryCriteria criteria, Pageable pageable){
        Page<GzmuAnnUserContacts> page = gzmuAnnUserContactsRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(gzmuAnnUserContactsMapper::toDto));
    }

    @Override
    public List<GzmuAnnUserContactsDto> queryAll(GzmuAnnUserContactsQueryCriteria criteria){
        return gzmuAnnUserContactsMapper.toDto(gzmuAnnUserContactsRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public GzmuAnnUserContactsDto findById(Long id) {
        GzmuAnnUserContacts gzmuAnnUserContacts = gzmuAnnUserContactsRepository.findById(id).orElseGet(GzmuAnnUserContacts::new);
        ValidationUtil.isNull(gzmuAnnUserContacts.getId(),"GzmuAnnUserContacts","id",id);
        return gzmuAnnUserContactsMapper.toDto(gzmuAnnUserContacts);
    }


    /*姓名查找通讯录*/
    @Override
    public List<Map<String,Object>> findContactsByName(Long myId,String name,Pageable pageable) {
        return gzmuAnnUserContactsRepository.findContactsByName(myId,name,pageable);
    }

    /*通过用户id查找申请通讯录的用户列表*/
    @Override
    public List<Map<String,Object>> findContactsApply(Long myId,Pageable pageable) {
        return gzmuAnnUserContactsRepository.findContactsApply(myId,pageable);
    }

    /*通过用户id查找申请通讯录的用户列表*/
    @Override
    public void alterByContactsId(Long contactId, Integer consent) {
        gzmuAnnUserContactsRepository.alterByContactsId(contactId,consent);
    }

    /*通过用户id查找申请通讯录的用户列表*/
    @Override
    public GzmuAnnUserContacts findByUserId(Long myId,Long userId) {
        return gzmuAnnUserContactsRepository.findByUserId(myId,userId);
    }

    @Override
    public GzmuAnnUserContacts findByMyId(Long myId, Long userId) {
        return gzmuAnnUserContactsRepository.findByMyId(myId,userId);
    }

    /*查询通讯录信息*/
    @Override
    public List<Map<String,Object>> findByMyId(Long myId,Pageable pageable) {
        List<Map<String,Object>> ts = gzmuAnnUserContactsRepository.findByTeaMyId(myId,pageable);
        List<Map<String,Object>> ss = gzmuAnnUserContactsRepository.findByStuMyId(myId,pageable);
        for (int i=0;i<ss.size();i++){
            ts.add(ss.get(i));
        }
        return ts;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public GzmuAnnUserContactsDto create(GzmuAnnUserContacts resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setId(snowflake.nextId());
        return gzmuAnnUserContactsMapper.toDto(gzmuAnnUserContactsRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(GzmuAnnUserContacts resources) {
        GzmuAnnUserContacts gzmuAnnUserContacts = gzmuAnnUserContactsRepository.findById(resources.getId()).orElseGet(GzmuAnnUserContacts::new);
        ValidationUtil.isNull( gzmuAnnUserContacts.getId(),"GzmuAnnUserContacts","id",resources.getId());
        gzmuAnnUserContacts.copy(resources);
        gzmuAnnUserContactsRepository.save(gzmuAnnUserContacts);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            gzmuAnnUserContactsRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<GzmuAnnUserContactsDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (GzmuAnnUserContactsDto gzmuAnnUserContacts : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" userId",  gzmuAnnUserContacts.getUserId());
            map.put(" userContactsId",  gzmuAnnUserContacts.getUserContactsId());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
